/*
 * FLINT PARTICLE SYSTEM
 * .....................
 * 
 * Author: Richard Lord (Big Room)
 * C# Port: Ben Baker (HeadSoft)
 * Copyright (c) Big Room Ventures Ltd. 2008
 * http://flintparticles.org
 * 
 * 
 * Licence Agreement
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Windows;
using System.Windows.Media;
using FlintSharp.Actions;
using FlintSharp.Activities;
using FlintSharp.Counters;
using FlintSharp.Easing;
using FlintSharp.Emitters;
using FlintSharp.EnergyEasing;
using FlintSharp.Initializers;
using FlintSharp.Particles;
using FlintSharp.Zones;

namespace FlintSharp.Particles
{
    /// <summary>
	/// The Particle public class is a set of public properties shared by all particles.
	/// It is deliberately lightweight, with only one method. The Initializers
	/// and Actions modify these properties directly. This means that the same
	/// particles can be used in many different emitters, allowing Particle 
	/// objects to be reused.
	/// 
	/// Particles are usually created by the ParticleCreator public class. This public class
	/// just simplifies the reuse of Particle objects which speeds up the
	/// application. 
    /// </summary>
    public class Particle : IComparable<Particle>
    {
    	/// <summary>
        /// Creates a particle. Alternatively particles can be reused by using the ParticleCreator to create
        /// and manage them. Usually the emitter will create the particles and the user doesn't need
        /// to create them.
        /// </summary>
        public Particle()
        {
        	IsDead = false;
        	Energy = 1;
        	Age = 0;
        	Lifetime = 0;
        	Scale = 1;
        	Color = 0xFFFFFFFF;
        	AngularVelocity = 0;
        	Rotation = 0;
        	VelocityY = 0;
        	VelocityX = 0;
        	Y = 0;
        	X = 0;
        }

    	/// <summary>
        /// Sets the particles properties to their default values.
        /// </summary>
        public void Initialize()
        {
            X = 0;
            Y = 0;
            VelocityX = 0;
            VelocityY = 0;
            Rotation = 0;
            AngularVelocity = 0;
            Color = 0xFFFFFFFF;
            Scale = 1;
            Lifetime = 0;
            Age = 0;
            Energy = 1;
            IsDead = false;
        }

        /// <summary>
        /// A transformation matrix for the position, scale and rotation of the particle.
        /// </summary>
        public Matrix MatrixTransform()
        {
            float cos = (float)(Scale * Math.Cos(Rotation));
            float sin = (float)(Scale * Math.Sin(Rotation));

            return new Matrix(cos, sin, -sin, cos, X, Y);
        }

    	/// <summary>
    	/// The x coordinate of the particle in pixels.
    	/// </summary>
		public double X { get; set; }

    	/// <summary>
    	/// The y coordinate of the particle in pixels.
    	/// </summary>
		public double Y { get; set; }

    	/// <summary>
    	/// The x coordinate of the velocity of the particle in pixels per second.
    	/// </summary>
		public double VelocityX { get; set; }

    	/// <summary>
    	/// The y coordinate of the velocity of the particle in pixels per second.
    	/// </summary>
		public double VelocityY { get; set; }

    	/// <summary>
    	/// The rotation of the particle in radians.
    	/// </summary>
		public double Rotation { get; set; }

    	/// <summary>
    	/// The angular velocity of the particle in radians per second.
    	/// </summary>
		public double AngularVelocity { get; set; }

    	/// <summary>
    	/// The 32bit ARGB color of the particle. The initial value is 0xFFFFFFFF (white).
    	/// </summary>
    	public uint Color { get; set; }

    	/// <summary>
    	/// The scale of the particle ( 1 is normal size ).
    	/// </summary>
		public float Scale { get; set; }

    	/// <summary>
    	/// The lifetime of the particle, in seconds.
    	/// </summary>
    	public float Lifetime { get; set; }

    	/// <summary>
    	/// The age of the particle, in seconds.
    	/// </summary>
    	public float Age { get; set; }

    	/// <summary>
    	/// The energy of the particle.
    	/// </summary>
    	public float Energy { get; set; }

    	/// <summary>
    	/// Whether the particle is dead and should be removed from the stage.
    	/// </summary>
    	public bool IsDead { get; set; }

    	/// <summary>
        /// For sorting the particles by x
        /// </summary>
        public int CompareTo(Particle other)
        {
            return X.CompareTo(other.X);
        }
    }
}
